﻿using System;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryDiagnosis: ADONetRepositoryBase<Diagnosis>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Diagnosis entity)
        {
            var cmd = new SqlCommand("DiagnosisInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("SurveyId", entity.Survey.Id));
            logger.Trace("SqlCommand для записи данных Диагноза в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("DiagnosisDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Диагноза id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Diagnosis entity)
        {
            var cmd = new SqlCommand("DiagnosisUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("SurveyId", entity.Survey.Id));
            logger.Trace("SqlCommand для обновления данных Диагноза id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetDiagnosis", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Диагноза id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetDiagnosisByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Диагноза {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Diagnosis GetEntity(SqlDataReader sqlDataReader)
        {
            IRepository<Survey> adoNetRepositorySurvey = new ADONetRepositorySurvey();
            Diagnosis diagnosis = new Diagnosis();
            diagnosis.Id = (int)sqlDataReader["Id"];
            diagnosis.Name = sqlDataReader["Name"].ToString();
            diagnosis.Survey = adoNetRepositorySurvey.GetById((int)sqlDataReader["SurveyId"]);
            return diagnosis;
        }
    }
}
